from collections import defaultdict
from typing import *


class Solution:
    def findHighAccessEmployees(self, access_times: List[List[str]]) -> List[str]:
        h = {}

        def f(t):
            return int(t[:2]) * 60 + int(t[2:])

        for n, t in access_times:
            if n in h:
                h[n].append(f(t))
            else:
                h[n] = [f(t)]
        ans = []
        for k, v in h.items():
            if len(v) >= 3:
                v = sorted(v)
                for i in range(2, len(v)):
                    if v[i] - v[i - 2] < 60:
                        ans.append(k)
                        break
        return ans


s = Solution()
print(
    s.findHighAccessEmployees(
        access_times=[
            ["akuhmu", "0454"],
            ["aywtqh", "0523"],
            ["akuhmu", "0518"],
            ["ihhkc", "0439"],
            ["ihhkc", "0508"],
            ["akuhmu", "0529"],
            ["aywtqh", "0530"],
            ["aywtqh", "0419"],
        ]
    )
)
print(
    s.findHighAccessEmployees(
        access_times=[
            ["a", "0549"],
            ["b", "0457"],
            ["a", "0532"],
            ["a", "0621"],
            ["b", "0540"],
        ]
    )
)


class Solution:
    def findHighAccessEmployees(self, access_times: List[List[str]]) -> List[str]:
        name2times = defaultdict(list)  # 用defaultdict 简洁
        for name, s in access_times:
            t = int(s[:2]) * 60 + int(s[2:])
            name2times[name].append(t)

        ans = []
        for name, a in name2times.items():
            a.sort()
            if any(a[i] - a[i - 2] < 60 for i in range(2, len(a))):  # 用any 简洁
                ans.append(name)
        return ans


# 作者：灵茶山艾府
# 链接：https://leetcode.cn/problems/high-access-employees/solutions/2523223/an-zhao-ming-zi-fen-zu-pai-xu-pythonjava-fkax/
# 来源：力扣（LeetCode）
# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
